# encoding: utf-8
# @File  : test02_login_fail.py
# @Author: wu shaofan
# @Date  :  2024/04/17
# @Desc : 前台文web端登录页面，用户登录失败用例
import json
import allure
import pytest
import conftest
from Utils.get_driver import GetDriver
from PO.page.page_login import PageLogin
from Utils.get_data import CreateData
from Utils.record_log import Log
from config import login_url


@allure.feature('1、登录模块')
class TestLogin:
    def setup_class(self):
        # 获取驱动对象
        self.driver = GetDriver().get_driver(login_url)
        # 异常截图
        conftest.driver = self.driver
        # 获取登录页面对象
        self.login = PageLogin(self.driver)
        # 获取日志对象
        self.log = Log("登录失败用例").get_logger()

    def teardown_class(self):
        # 关闭驱动对象
        GetDriver().close_driver()
        Log("登录成功用例").write_empty_line()

    # 参数化数据
    json_name = "login"
    data = ('username', 'pwd', 'code', 'expect')
    obj = CreateData(json_name, data)
    data = obj.build_data()
    # 用例标题
    ids_data = obj.build_desc()

    @allure.story('2、登录失败')
    @pytest.mark.parametrize("username,pwd,code,expect", data, ids=ids_data)
    def test_login02(self, username, pwd, code, expect):

        with allure.step("测试步骤一：输入用户名"):
            self.login.page_input_username(username)

        with allure.step("测试步骤二：输入密码"):
            self.login.page_input_password(pwd)

        with allure.step("测试步骤三：输入验证码"):
            self.login.page_input_verify_code(code)

        with allure.step("测试步骤四：点击登录按钮"):
            self.login.page_click_login_btn()

        with allure.step("测试步骤五：获取弹出框中的提示信息"):
            info = self.login.page_get_login_alert_info()
            self.login.base_get_allure_image()

        with allure.step("断言结果六：点击提示框确定按钮"):
            self.login.page_click_login_alert_confirm_btn()

        assert info == expect

        data = {
            "用户名": username,
            "密码": pwd,
            "验证码": code,
            "断言": {
                "预期结果": expect,
                "实际结果": info}}
        self.log.info(f"测试数据：{data}")
        data = json.dumps(data, ensure_ascii=False, indent=4)
        allure.attach(data, "测试数据", allure.attachment_type.JSON)


if __name__ == '__main__':

    pytest.main()
